<?php

/*
    Author: Mike
    Date:2013/08/22
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

//礼品卡列表

if ($_REQUEST['act'] == 'list')
{
    $smarty->assign('full_page',    1);
    $gift_card_list = get_gift_card_list();
    $smarty->assign('gift_card_list',   $gift_card_list['gift_card']);
    $smarty->assign('filter',       $gift_card_list['filter']);
    $smarty->assign('record_count', $gift_card_list['record_count']);
    $smarty->assign('page_count',   $gift_card_list['page_count']);
    
    $smarty->display('gift_card.htm');
}
elseif ($_REQUEST['act'] == 'query')
{
    $gift_card_list = get_gift_card_list();
    $smarty->assign('gift_card_list',   $gift_card_list['gift_card']);
    $smarty->assign('filter',       $gift_card_list['filter']);
    $smarty->assign('record_count', $gift_card_list['record_count']);
    $smarty->assign('page_count',   $gift_card_list['page_count']);

    make_json_result($smarty->fetch('gift_card.htm'), '',array('filter' => $gift_card_list['filter'], 'page_count' => $gift_card_list['page_count']));
}

elseif($_REQUEST['act'] == 'using')
{
    admin_priv('bonus_activation');
    $bonus_id = intval($_GET['id']);
    $bonus_number = get_gift_card_info($bonus_id);
    $db->query("update" .$GLOBALS['ecs']->table('user_bonus'). " set is_confirmed='1' WHERE bonus_id=$bonus_id");
    admin_log($bonus_id."--".$bonus_number, 'activate', 'gift_card');
    $url = 'gift_card.php?act=query&' . str_replace('act=using', '', $_SERVER['QUERY_STRING']);
    ecs_header("Location: $url\n");
    exit;
}

elseif($_REQUEST['act'] == 'stop')
{
    admin_priv('bonus_activation');
    $bonus_id = intval($_GET['id']);
    $bonus_number = get_gift_card_info($bonus_id);
    $db->query("update" .$GLOBALS['ecs']->table('user_bonus'). " set is_confirmed='0' WHERE bonus_id=$bonus_id");  
    admin_log($bonus_id."--".$bonus_number, 'desactivate', 'gift_card');
    $url = 'gift_card.php?act=query&' . str_replace('act=stop', '', $_SERVER['QUERY_STRING']);
    ecs_header("Location: $url\n");
    exit;
}

elseif ($_REQUEST['act'] == 'remove')
{
    admin_priv('bonus_activation');
    $bonus_id = intval($_GET['id']);
    $bonus_number = get_gift_card_info($bonus_id);
    $db->query("DELETE from" .$GLOBALS['ecs']->table('user_bonus'). " WHERE bonus_id=$bonus_id");
    admin_log($bonus_id."--".$bonus_number, 'remove', 'gift_card');    
    $url = 'gift_card.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);
    ecs_header("Location: $url\n");
    exit;
}
//获取礼品卡列表
function get_gift_card_list()
{
    $result = get_filter();
    if ($result === false)
    {
        //添加搜索功能 date:0827
        $aiax = isset($_GET['is_ajax']) ? $_GET['is_ajax'] : 0;
        if ($aiax == 1 && !empty($_REQUEST['giftcard_name']))
        {
            $_REQUEST['giftcard_name'] = json_str_iconv($_REQUEST['giftcard_name']);
        }
        $filter['giftcard_name'] = empty($_REQUEST['giftcard_name']) ? '' : trim($_REQUEST['giftcard_name']);
        $where = '';
        if ($filter['giftcard_name'])
        {
            $where .= " AND bonus_number LIKE '%" . mysql_like_quote($filter['giftcard_name']) . "%'";
        }
        
        
        /* 分页大小 */
        $filter = array();

        /* 记录总数以及页数 */

        $count = "SELECT count(u.bonus_id)".
            " FROM " .$GLOBALS['ecs']->table('user_bonus'). " AS u".
            " left JOIN ".$GLOBALS['ecs']->table('bonus_type'). " AS b".
            " ON u.bonus_type_id = b.type_id "." WHERE b.send_type = '4'".$where;
        
        $sql = "SELECT u.bonus_id,u.bonus_balance,u.bonus_number,u.user_id,u.used_time,u.is_confirmed,b.type_money,b.use_end_date,a.user_name".
            " FROM " .$GLOBALS['ecs']->table('user_bonus'). " AS u ".
            " left JOIN ".$GLOBALS['ecs']->table('bonus_type'). " AS b ON u.bonus_type_id = b.type_id".
            " LEFT JOIN ".$GLOBALS['ecs']->table('users'). " AS a ".
            " ON u.user_id = a.user_id "." WHERE b.send_type = '4'".$where." order by u.bonus_id desc";
            
        $filter['record_count'] = $GLOBALS['db']->getOne($count);

        $filter = page_and_size($filter);

        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);

    $arr = array();

    while ($rows = $GLOBALS['db']->fetchRow($res))
    {
        $rows['used_time'] = date("Y-m-d",$rows['used_time']);
        $rows['use_end_date'] = date("Y-m-d",$rows['use_end_date']);
        $arr[] = $rows;
    }

    return array('gift_card' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
    
}

/* 获取礼品卡信息 */
function get_gift_card_info($bonus_id){
    $bonus_id = compile_str($bonus_id);
    $sql = " SELECT bonus_number FROM ".
            $GLOBALS['ecs']->table('user_bonus').
            " WHERE bonus_id ='".$bonus_id."'";
    
    $bonu_number = $GLOBALS['db']->getOne($sql);
    return $bonu_number;
}



?>